package com.itcast.n1_内存调优;

import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: jvm
 * @description: -Xms1g -Xmx1g
 * @author: flyduck
 * @create: 2024-10-16 17:44
 **/
@RestController
@RequestMapping("/leak3")
public class Test17_内存泄漏问题5_内存泄漏导致内存溢出 {

    private static Map<Long,Object> userCache = new HashMap<>();

    /**
     * 登录接口 放入hashmap中
     */
    @PostMapping("/login")
    public void login(String name,Long id){
        userCache.put(id,new UserEntity1(id,name));
    }
}

@Data
class UserEntity1 {
    private Long id;
    private String name;
    //每个对象增加1MB的额外内存，让溢出来的更快一些
    private byte[] bytes = new byte[1024 * 1024];

    public UserEntity1(Long id, String name) {
        this.id = id;
        this.name = name;
    }
}
